﻿//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：EditorCreatPage
// *描述：EditorCreatPage
// *?2024 HDMCode . All rights reserved.
//* ======================================================================
using HDMCode.FrameEditor;
using UnityEditor;
using UnityEngine;

namespace HDMCode.UIFrameEditor
{
    public class EditorCreatPage: EditorWindow
    {
        /// <summary>
        /// 当前编辑器窗口
        /// </summary>
        public static EditorCreatPage ThisWindow { get; set; }
        
        /// <summary>
        /// 当前创建数据
        /// </summary>
        EditorCreatDate m_CreatDate = new (); 
        
        /// <summary>
        /// Editor初始化生命周期
        /// </summary>
        public void OnEnable()
        {
           ThisWindow ??= this;
           ThisWindow.maxSize = new Vector2(250, 300);
           ThisWindow.minSize = ThisWindow.maxSize;
           HDMEditorStyle.InitStyle();
        }

        public void OnGUI()
        {
            GUI.color = new Color(255, 129, 203);
            GUILayout.Space(10);
            GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(2));
            GUILayout.Box("UI创建", new[] { GUILayout.ExpandWidth(true), GUILayout.ExpandWidth(true) });
            GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(2));
            GUI.color = Color.white;
            UnityEditor.EditorGUILayout.Space();
            
            using (new UnityEditor.EditorGUILayout.HorizontalScope())
            {
                Texture tileIcon = EditorGUIUtility.IconContent("cs Script Icon").image;
                GUILayout.Label(tileIcon, GUILayout.Width(20), GUILayout.Height(18));
                GUILayout.Label("资源名称：");
                m_CreatDate.CreatName = UnityEditor.EditorGUILayout.TextField("", m_CreatDate.CreatName , GUILayout.Width(150), GUILayout.Height(20));
            }
            HDMEditorHelper.DrawXLine();
            GUILayout.Space(5);
            
            using (new UnityEditor.EditorGUILayout.HorizontalScope())
            {
                Texture tileIcon = EditorGUIUtility.IconContent("d_console.infoicon").image;
                GUILayout.Label(tileIcon, GUILayout.Width(20), GUILayout.Height(18));
                GUILayout.Label("资源描述：");
                m_CreatDate.Describe = UnityEditor.EditorGUILayout.TextField("", m_CreatDate.Describe , GUILayout.Width(150), GUILayout.Height(20));
            }
            HDMEditorHelper.DrawXLine();
            GUILayout.Space(5);
            
            using (new UnityEditor.EditorGUILayout.HorizontalScope())
            {
                Texture tileIcon = EditorGUIUtility.IconContent("d_Particle Effect").image;
                GUILayout.Label(tileIcon, GUILayout.Width(20), GUILayout.Height(18));
                GUILayout.Label("选择分组：");
                EditorHelper.CreatEmun(m_CreatDate.Group,EditorBaseDate.Setting.Groups, (re) =>
                {
                    m_CreatDate.Group = re;
                });
            }
            HDMEditorHelper.DrawXLine();
            GUILayout.Space(5);
            
            using (new UnityEditor.EditorGUILayout.HorizontalScope())
            {
                Texture tileIcon = EditorGUIUtility.IconContent("d_Particle Effect").image;
                GUILayout.Label(tileIcon, GUILayout.Width(20), GUILayout.Height(18));
                GUILayout.Label("创建时加载到场景中：");
                m_CreatDate.IsLoadToScene = GUILayout.Toggle(m_CreatDate.IsLoadToScene, "");
            }
            HDMEditorHelper.DrawXLine();
            GUILayout.Space(5);
            
            GUILayout.Space(10);
            if (GUILayout.Button("创建UI资源",GUILayout.Height(30)))
            {
                CreaAssetsAndScript();
            }
            
            if (GUI.changed)
                ThisWindow.SaveChanges();
        }

        /// <summary>
        /// 创建脚本资源和预制件资源
        /// </summary>
        public void CreaAssetsAndScript()
        {
            if (string.IsNullOrEmpty(EditorBaseDate.Setting.AssetsPath)||string.IsNullOrEmpty(EditorBaseDate.Setting.ScriptPath))
            {
                EditorUtility.DisplayDialog("错误提示：", $"创建失败！资源保存路径或脚本保存路径为空！", "确定");
                return;
            }
            
            if (string.Equals(EditorBaseDate.Setting.AssetsPath,EditorBaseDate.Setting.ScriptPath))
            {
                EditorUtility.DisplayDialog("错误提示：", $"创建失败！资源保存路径不可和脚本保存路径为同一路径！", "确定");
                return;
            }
            
            if (EditorMenu.Handle.CheckFloder(m_CreatDate,out var path))
            {
                EditorUtility.DisplayDialog("错误提示：", $"创建失败！已存在该创建信息!\n路径：【{path}】", "确定");
                return;
            }

            if (EditorHelper.ContainsSetting(m_CreatDate.CreatName,out var value))
            {
                EditorUtility.DisplayDialog("错误提示：", $"创建失败！已存在该历史记录！\n名称：【{value.CreatName}】", "确定");
                return;
            }

            if (EditorMenu.Handle.CheckFile(m_CreatDate))
            {
                EditorUtility.DisplayDialog("错误提示：", $"创建失败！\n已存在该资源文件!\n名称：【{m_CreatDate.CreatName}】", "确定");
                return;
            }
            
            var uIDisplay = EditorMenu.Handle.CreatPrefabAssets(m_CreatDate);
            EditorMenu.Handle.CreatScriptAssets(m_CreatDate,uIDisplay.components);
            EditorMenu.Handle.AddInfoToSetting(m_CreatDate);
            uIDisplay.uISingleName = m_CreatDate.CreatName;
            uIDisplay.scriptDescribe = m_CreatDate.Describe;
            m_CreatDate = new();
        }
    }
}
